package hr.ctg.web.servlet;

import hr.ctg.domain.model.Dogadjaj;
import hr.ctg.domain.model.Opis;
import hr.ctg.service.dao.DogadjajDAO;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.seam.Component;

/**
 * Servlet implementation class DogadjajServlet
 */
public class DogadjajServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final SimpleDateFormat sdf = new SimpleDateFormat(
			"MMM dd yyyy hh:mm:ss", Locale.US);

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		response.setContentType("text/xml");
		final PrintWriter pw = response.getWriter();
		final StringBuilder sb = new StringBuilder();
		try {
			final String idMjesta = request.getParameter("idMjesta");
			final String idOtoka = request.getParameter("idOtoka");
			final String idZupanije = request.getParameter("idZupanije");
			final String lang = request.getParameter("lang");
			final DogadjajDAO dogadjajDAO = (DogadjajDAO) Component
					.getInstance("dogadjajDAO");
			List<Dogadjaj> l;
			if (idMjesta != null && !idMjesta.equals("")) {
				l = dogadjajDAO.findByIdMjesta(Integer.parseInt(idMjesta));
			} else if (idZupanije != null && !idZupanije.equals("")) {
				l = dogadjajDAO.findByIdZupanije(Integer.parseInt(idZupanije));
			} else if (idOtoka != null && !idOtoka.equals("")) {
				l = dogadjajDAO.findByIdOtoka(Integer.parseInt(idOtoka));
			} else {
				l = dogadjajDAO.findAll();
			}

			sb.append("<data>");
			for (final Dogadjaj dogadjaj : l) {
				Opis opis = null;
				for (final Opis o : dogadjaj.getOpisDogadjajas()) {
					if (o.getJezik().getOznaka().equals(lang)) {
						opis = o;
					}
				}
				sb.append("<event");
				sb.append(" start=\"" + sdf.format(dogadjaj.getDatumOd())
						+ " GMT\"");
				if (dogadjaj.getDatumDo() != null) {
					sb.append(" end=\""
							+ (dogadjaj.getDatumDo() != null ? sdf
									.format(dogadjaj.getDatumDo())
									+ " GMT" : "") + "\"");
				}
				sb.append(" isDuration=\"" + dogadjaj.isTrajanje() + "\"");
				sb.append(" title=\"" + (opis != null ? opis.getNaziv() : "")
						+ "\"");
				sb.append(" link=\"" + dogadjaj.getWww() + "\"");
				sb.append(" image=\"\">");
				sb.append((opis != null ? opis.getOpis() : ""));
				sb.append("</event>");
			}
			sb.append("</data>");

		} catch (final NoClassDefFoundError e) {
			sb.append("<data>");
			sb.append("<event ");
			sb.append(" start=\"Aug 02 2006 00:00:00 GMT\"");
			sb.append(" title=\"Trip to Beijing\"");
			sb.append(" link=\"http://travel.yahoo.com/\"");
			sb.append(">Woohoo!");
			sb.append("</event>");
			sb.append("</data>");
		}
		pw.print(sb.toString());
		pw.close();
	}
}
